package com.harvest.scientific.ladder.page

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.annotation.RequiresApi
import com.harvest.scientific.ladder.R
import com.harvest.scientific.ladder.base.BaActivity
import com.harvest.scientific.ladder.databinding.ActWebBinding
import com.harvest.scientific.ladder.ext.getString
import com.harvest.scientific.ladder.util.LogUtils
import com.harvest.scientific.ladder.util.StbarUtil


class WebViewActivity: BaActivity() {

    private lateinit var binding: ActWebBinding
    private val TAG = "WebViewActivity"

    private var url: String? = null
    private var title: String? = null

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StbarUtil.setStatusBar(this, true, Color.TRANSPARENT, true)
        binding = ActWebBinding.inflate(layoutInflater)
        setContentView(binding.root)
        url = intent.getStringExtra("url") ?: ""
        title = intent.getStringExtra("title") ?: ""

        if (url.isNullOrEmpty()) {
            onBackPressed()
            return
        }
        binding.actionBar.setBlackMode(true)
        binding.actionBar.setTitle(title ?: "")
        binding.actionBar.isShowBusinessServer(false)
        binding.actionBar.setBackListener{
            onBackPressed()
        }

        initSetting()
        binding.webView.setWebViewClient(object : WebViewClient() {

            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                try {
                    if (!url.startsWith("http") && !url.startsWith("https")) {
                        if (url.startsWith("whatsapp://")){
                        }
                        try {
                        } catch (e: Exception) {
                            LogUtils.d(TAG, "e:${e.stackTraceToString()}")
                        }
                        return true;
                    }

                } catch (e: Exception) {
                    LogUtils.d(TAG, "e2:${e.stackTraceToString()}")
                }
                return super.shouldOverrideUrlLoading(view, url);
            }


            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
            }
        })

        binding.webView.loadUrl(url!!)
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initSetting() {
        val settings = binding.webView.settings
        settings.domStorageEnabled = true
        settings.javaScriptEnabled = true
        settings.useWideViewPort = true
        settings.loadWithOverviewMode = true
        settings.setSupportZoom(true)
        settings.builtInZoomControls = true
        settings.displayZoomControls = true
        settings.javaScriptCanOpenWindowsAutomatically = true;
        settings.setSupportMultipleWindows(true)

    }


    companion object {

        val protocol = "https://test.fancyargent.com/fancyargents/privacy.html"

        fun jumProtocol(context: Context) {
            context.startActivity(
                Intent(context, WebViewActivity::class.java)
                    .apply {
                        putExtra("url", protocol)
                        putExtra("title", getString(R.string.spa_6))
                    }
            )
        }

        fun jump(context: Context, url: String, title: String? = null) {
            context.startActivity(
                Intent(context, WebViewActivity::class.java)
                    .apply {
                        putExtra("url", url)
                        putExtra("title", title)
                    }
            )
        }
    }
}